﻿@page "/Admin/Dicts"
@attribute [Authorize]
@attribute [TabItemOption(Text ="字典管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysDictTypeService SysDictTypeService
@inject ISysDictDataService SysDictDataService

<div class="overflow-hidden h-100">
    <Table TItem="SysDictType"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="true"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_searchType" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="250"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryTypeAsync"
            OnSaveAsync="@OnSaveTypeAsync"
            OnDeleteAsync="@OnDeleteTypeAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" />
            <TableColumn @bind-Field="@context.Code" />
            <TableColumn @bind-Field="@context.Sort" />
            <TableColumn @bind-Field="@context.Remark" />
            <TableColumn @bind-Field="@context.Status" />
        </TableColumns>
        <RowButtonTemplate>
            <TableCellButton Color="Color.Primary" Icon="fa fa-edit" Text="字典" OnClick="@(() => OnRowButtonClick(context))" />
        </RowButtonTemplate>
        <EditTemplate>
            <div class="row g-3 form-inline">
                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                </div>

                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                </div>

                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" />
                </div>

                <div class="col-12 col-sm-6 col-md-12" style="display:inline-flex;">
                    <Textarea @bind-Value="@context.Remark" maxlength="50" />
                </div>
            </div>
        </EditTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                    </div>
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.Code" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

<Modal @ref="_modal">
    <ModalDialog Title="字典值管理" IsCentered="true">
        <BodyTemplate>
            <Table TItem="DictDataOutput" 
                    @ref = "@_table"
                    Height="400"
                    IsPagination="true"
                    IsStriped="true"
                    IsMultipleSelect="true"
                    AllowResizing="true"
                    ShowLoading="true"
                    ShowSearch="true"
                    ShowToolbar="true"
                    ShowEmpty="true" 
                    SearchModel="@_searchData" 
                    SearchMode="SearchMode.Top"
                    ShowExtendButtons="true" 
                    EditDialogItemsPerRow="1"
                    ExtendButtonColumnWidth="250"
                    PageItemsSource="@(new int[] { 15, 50 })"
                    OnQueryAsync="@OnQueryDataAsync"
                    OnSaveAsync="@OnSaveDataAsync"
                    OnDeleteAsync="@OnDeleteDataAsync">
                <TableColumns>
                    <TableColumn @bind-Field="@context.Value" />
                    <TableColumn @bind-Field="@context.Code" />
                    <TableColumn @bind-Field="@context.Sort" />
                    <TableColumn @bind-Field="@context.Remark" />
                    <TableColumn @bind-Field="@context.Status" />
                </TableColumns>
                <SearchTemplate>
                    <GroupBox Title="搜索条件">
                        <div class="row g-3 form-inline">
                            <div class="col-12 col-sm-6">
                                <BootstrapInput @bind-Value="@context.Value" ShowLabel="true"/>
                            </div>
                            <div class="col-12 col-sm-6">
                                <BootstrapInput @bind-Value="@context.Code" ShowLabel="true"/>
                            </div>
                        </div>
                    </GroupBox>
                </SearchTemplate>
            </Table>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code{
    private Modal? _modal;
    private Table<DictDataOutput>? _table;
    private SysDictType _searchType = new();
    private DictDataOutput _searchData = new();

    private Task<QueryData<SysDictType>> OnQueryTypeAsync(QueryPageOptions options)
        => SysDictTypeService.PageAsync(_searchType.Adapt<DictTypePageInput>(options));

    private Task<bool> OnDeleteTypeAsync(IEnumerable<SysDictType> items)
        => SysDictTypeService.DeleteAsync(items);

    private async Task<bool> OnSaveTypeAsync(SysDictType item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysDictTypeService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysDictTypeService.EditAsync(item);
                break;
        }
        return result;
    }

    private async Task OnRowButtonClick(SysDictType item)
    {
        _searchData = new() { TypeId = item.Id };
        await _modal!.Toggle();
        await _table!.QueryAsync();
    }

    private Task<QueryData<DictDataOutput>> OnQueryDataAsync(QueryPageOptions options)
        => SysDictDataService.PageAsync(_searchData.Adapt<DictDataPageInput>(options));

    private Task<bool> OnDeleteDataAsync(IEnumerable<DictDataOutput> items)
        => SysDictDataService.DeleteAsync(items);

    private async Task<bool> OnSaveDataAsync(DictDataOutput item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysDictDataService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysDictDataService.EditAsync(item);
                break;
        }
        return result;
    }
}